package com.mazaiting.classfile.attr;

import com.mazaiting.classfile.attr.info.*;
import com.mazaiting.classfile.constant.ConstantPool;

/**
 * 属性工厂
 *
 * @author mazaiting
 * @since 2021/8/12
 */
public class AttributeFactory {
    public static final String CONSTANT_VALUE = "ConstantValue";
    public static final String CODE = "Code";
    public static final String EXCEPTIONS = "Exceptions";
    public static final String SOURCE_FILE = "SourceFile";
    public static final String LINE_NUMBER_TABLE = "LineNumberTable";
    public static final String LOCAL_VARIABLE_TABLE = "LocalVariableTable";
    public static final String INNER_CLASSES = "InnerClasses";
    public static final String SYNTHETIC = "Synthetic";
    public static final String DEPRECATED = "Deprecated";
    public static final String ENCLOSING_METHOD = "EnclosingMethod";
    public static final String SIGNATURE = "Signature";
    public static final String SOURCE_DEBUG_EXTENSION = "SourceDebugExtension";
    public static final String LOCAL_VARIABLE_TYPE_TABLE = "LocalVariableTypeTable";
    public static final String RUNTIME_VISIBLE_ANNOTATIONS = "RuntimeVisibleAnnotations";
    public static final String RUNTIME_INVISIBLE_ANNOTATIONS = "RuntimeInvisibleAnnotations";
    public static final String RUNTIME_VISIBLE_PARAMETER_ANNOTATIONS = "RuntimeVisibleParameterAnnotations";
    public static final String RUNTIME_INVISIBLE_PARAMETER_ANNOTATIONS = "RuntimeInvisibleParameterAnnotations";
    public static final String ANNOTATION_DEFAULT = "AnnotationDefault";
    public static final String STACK_MAP_TABLE = "StackMapTable";
    public static final String BOOTSTRAP_METHODS = "BootstrapMethods";
    public static final String RUNTIME_VISIBLE_TYPE_ANNOTATIONS = "RuntimeVisibleTypeAnnotations";
    public static final String RUNTIME_INVISIBLE_TYPE_ANNOTATIONS = "RuntimeInvisibleTypeAnnotations";
    public static final String METHOD_PARAMETERS = "MethodParameters";

    /**
     * 创建属性信息
     *
     * @param name      名字
     * @param length    长度
     * @param pool      常量池
     * @param nameIndex 属性名索引
     * @return 属性信息
     */
    public static AttributeInfo newAttributeInfo(String name, int length, ConstantPool pool, int nameIndex) {
        switch (name) {
            case CONSTANT_VALUE:
                return new ConstantValueAttribute(pool, nameIndex, length);
            case CODE:
                return new CodeAttribute(pool, nameIndex, length);
            case EXCEPTIONS:
                return new ExceptionsAttribute(pool, nameIndex, length);
            case SOURCE_FILE:
                return new SourceFileAttribute(pool, nameIndex, length);
            case LINE_NUMBER_TABLE:
                return new LineNumberTableAttribute(pool, nameIndex, length);
            case LOCAL_VARIABLE_TABLE:
                return new LocalVariableTableAttribute(pool, nameIndex, length);
            case INNER_CLASSES:
                return new InnerClassesAttribute(pool, nameIndex, length);
            case SYNTHETIC:
                return new SyntheticAttribute(pool, nameIndex, length);
            case DEPRECATED:
                return new DeprecatedAttribute(pool, nameIndex, length);
            case ENCLOSING_METHOD:
                return new EnclosingMethodAttribute(pool, nameIndex, length);
            case SIGNATURE:
                return new SignatureAttribute(pool, nameIndex, length);
            case SOURCE_DEBUG_EXTENSION:
                return new SourceDebugExtensionAttribute(pool, nameIndex, length);
            case LOCAL_VARIABLE_TYPE_TABLE:
                return new LocalVariableTypeTableAttribute(pool, nameIndex, length);
            case RUNTIME_VISIBLE_ANNOTATIONS:
                return new RuntimeVisibleAnnotationsAttribute(pool, nameIndex, length);
            case RUNTIME_INVISIBLE_ANNOTATIONS:
                return new RuntimeInvisibleAnnotationsAttribute(pool, nameIndex, length);
            case RUNTIME_VISIBLE_PARAMETER_ANNOTATIONS:
                return new RuntimeVisibleParameterAnnotationsAttribute(pool, nameIndex, length);
            case RUNTIME_INVISIBLE_PARAMETER_ANNOTATIONS:
                return new RuntimeInvisibleParameterAnnotationsAttribute(pool, nameIndex, length);
            case ANNOTATION_DEFAULT:
                return new AnnotationDefaultAttribute(pool, nameIndex, length);
            case STACK_MAP_TABLE:
                return new StackMapTableAttribute(pool, nameIndex, length);
            case BOOTSTRAP_METHODS:
                return new BootstrapMethodsAttribute(pool, nameIndex, length);
            case RUNTIME_VISIBLE_TYPE_ANNOTATIONS:
                return new RuntimeVisibleTypeAnnotationsAttribute(pool, nameIndex, length);
            case RUNTIME_INVISIBLE_TYPE_ANNOTATIONS:
                return new RuntimeInvisibleTypeAnnotationsAttribute(pool, nameIndex, length);
            case METHOD_PARAMETERS:
                return new MethodParametersAttribute(pool, nameIndex, length);
            default:
                return new UnparsedAttribute(name, pool, nameIndex, length);
        }
    }
}


















